package com.efast.dmom.equipment.util;

import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.equipment.bean.checkplan.EquipCheckPlanBean;
import com.efast.dmom.equipment.entity.EquipCheckingTask;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class ExplanPlan {

    public static List<EquipCheckingTask> explanPlan(Date startDate, Date endDate,String unit, Integer number){
        List<EquipCheckingTask> list=new ArrayList<>();
        Calendar start = Calendar.getInstance();
        start.setTime(startDate);
        Calendar end = Calendar.getInstance();
        end.setTime(endDate);
        if(start.getTime().getTime()>end.getTime().getTime()){
            return list;
        }
        //根据计划时间和周期生成任务
        explanPlanTask(list,start,end,unit,number);
        return list;
    }

    public static void explanPlanTask(List<EquipCheckingTask> list,Calendar start,Calendar end,String unit,Integer number){
        EquipCheckingTask bean=new EquipCheckingTask();
        bean.setTaskStartTime(start.getTime());
        if("hour".equals(unit)){
            start.add(Calendar.HOUR_OF_DAY, number);
        }else if("day".equals(unit)){
            start.add(Calendar.DAY_OF_YEAR, number);
        }else if("week".equals(unit)){
            start.add(Calendar.WEEK_OF_YEAR, number);
        }else if("month".equals(unit)){
            start.add(Calendar.MONTH, number);
        }else if("quarter".equals(unit)){
            start.add(Calendar.MONTH, number*3);
        }else if("year".equals(unit)){
            start.add(Calendar.YEAR, number);
        }else{
            throw new ServiceException("周期单位错误");
        }
        if(start.getTime().getTime()<end.getTime().getTime()){
             bean.setTaskEndTime(start.getTime());
             list.add(bean);
            explanPlanTask(list,start,end,unit,number);
        }else{
            bean.setTaskEndTime(end.getTime());
            list.add(bean);
        }
    }
}
